package com.tuanzi.loan.business.service.shortlink.impl;

import com.tuanzi.loan.business.entity.shortlink.ShortLink;
import com.tuanzi.loan.business.repository.shortlink.ShortLinkRepository;
import com.tuanzi.loan.business.service.shortlink.ShortLinkService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.service.CommonServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author chenweijun 
* @version 2018年4月11日
*/
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class ShortLinkServiceImpl extends CommonServiceImpl<ShortLink, Long> implements ShortLinkService {
	
	@Autowired
	private ShortLinkRepository shortLinkRepository;

	@Override
	protected JpaRepository<ShortLink, Long> getRepository() {
		return shortLinkRepository;
	}

	@Override
	public ShortLink findByShortLink(String shortLink) {
		return shortLinkRepository.findByShortLink(shortLink);
	}

	@Override
	public ShortLink findByDisplayedShortLink(String displayedShortLink) {
		List<ShortLink> list = shortLinkRepository.findByDisplayedShortLinkOrderByCtimeDesc(displayedShortLink);
		if (CollectionUtils.isEmpty(list)) {
			return  null;
		}
		return list.get(0);
	}
}
